package com.xian.im.modules.sensitive;

import com.xian.im.common.proto.MsgRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: xian-code
 * @description: 敏感词过滤器
 * @author: liru.xian
 * @create: 2021-01-21 15:16
 **/
@Slf4j
@Component
public class SensitiveWordMessageFilterChain {

    private List<SensitiveWordMessageFilter> filters = new ArrayList<>();

    public SensitiveWordMessageFilterChain( List<SensitiveWordMessageFilter> wordMessageFilters){
        filters.addAll(wordMessageFilters);
    }
    /**
     *  添加过滤器
     * @param filter
     */
    public void addFilter(SensitiveWordMessageFilter filter){
        this.filters.add(filter);
    }

    /**
     *  过滤器执行入口
     * @param message
     * @return
     */
    public Boolean doFilter(MsgRequest message){
        for (SensitiveWordMessageFilter filter : filters) {
            if(!filter.doFilter(message)){
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

}
